package com.ruoyi.web.controller.glzz.ownSetPdf;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.IOException;

/**
 * @author linhx
 * @version 1.0
 * @description 水印
 * @date 2021/10/11 14:20
 */
public class Watermark extends PdfPageEventHelper {
    Font FONT = new Font(Font.FontFamily.HELVETICA, 30, Font.BOLD, new GrayColor(0.95f));
    //水印内容
    private String waterCont;

    public Watermark() {

    }

    public Watermark(String waterCont) {
        this.waterCont = waterCont;
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            float pageWidth = document.right() + document.left();//获取pdf内容正文页面宽度
            float pageHeight = document.top() + document.bottom();//获取pdf内容正文页面高度
            //设置水印字体格式
            BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font waterMarkFont = new Font(base, 20, Font.BOLD, BaseColor.LIGHT_GRAY);
            PdfContentByte waterMarkPdfContent = writer.getDirectContentUnder();
            //水印图片1
            Image image = Image.getInstance("D:/a.png");
            //水印图片2（印章）
            Image image2 = Image.getInstance("D:/a.png");
            // set the first background
            image.setAbsolutePosition(70, pageHeight - 33);
            //X 值越小 靠右， Y值越小 靠上
            image2.setAbsolutePosition(pageWidth - 120, pageHeight - 200);
            // image of the absolute
            //水印图片大小
            image.scaleToFit(200, 45);
            image2.scaleToFit(100, 100);
            waterMarkPdfContent.addImage(image);
            waterMarkPdfContent.addImage(image2);
        } catch (DocumentException | IOException de) {
            de.printStackTrace();
        }
    }
}

